package com.daiyang.transaction_boot.controller;

import com.daiyang.transaction_boot.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.beans.Transient;
import java.io.IOError;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
//    @Autowired
//    private DataSourceTransactionManager transactionManager;
//    @Autowired
//    private TransactionDefinition transactionDefinition;
    @RequestMapping("/r1")
    @Transactional
    public String r1(String username, String password) {
        //TransactionStatus transactionstatus = transactionManager.getTransaction(transactionDefinition);

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        int a=10/0;


       // transactionManager.commit(transactionstatus);
       // transactionManager.rollback(transactionstatus);
        return "注册成功";
    }

    @RequestMapping("/r2")
    @Transactional
    public String r2(String username, String password) {

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        try {
            int a=10/0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "注册成功";
    }
    @RequestMapping("/r3")
    @Transactional
    public String r3(String username, String password) {

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        try {
            int a=10/0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "注册成功";
    }
    @RequestMapping("/r4")
    @Transactional
    public String r4(String username, String password) {

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        try {
            int a=10/0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    @RequestMapping("/r5")
    @Transactional
    public String r5(String username, String password) throws IllegalAccessException {

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        if(true) {
            throw new IllegalAccessException();
        }
        return "注册成功";
    }
    @RequestMapping("/r6")
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.DEFAULT )
    public String r6(String username, String password) throws IllegalAccessException {

        userInfoService.userInfoInsert(username,password);
        log.info("用户数据插入成功");
        if(true) {
            throw new IllegalAccessException();
        }
        return "注册成功";
    }

}
